package com.lcx.plugin.neteaseCloud.component.toolwindow.login;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.util.Objects;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public abstract class AbstractPhoneDialogWrapper extends DialogWrapper {

    public JTextField textField;
    public JTextField countryCodeField;
    public JPanel panel;

    /**
     * 提示的标题
     *
     * @param
     * @return java.lang.String
     * @author daxiong
     * @date 2021/9/14 下午3:17
     */
    public abstract String getHead();

    /**
     * 校验不通过提示的信息
     *
     * @param
     * @return java.lang.String
     * @author daxiong
     * @date 2021/9/14 下午3:17
     */
    public abstract String getInfoMsg();

    /**
     * 底部提示信息
     *
     * @param
     * @return java.lang.String
     * @author daxiong
     * @date 2021/9/14 下午3:17
     */
    public abstract String getBottomMsg();

    /**
     * 是否是密码框
     *
     * @param
     * @return boolean
     * @author daxiong
     * @date 2021/10/22 下午5:34
     */
    public boolean isPassword() {
        return false;
    }

    /**
     * 要执行的函数
     *
     * @param text        手机号
     * @param countryCode 国家码
     * @return void
     * @author daxiong
     * @date 2021/9/14 下午3:17
     */
    public abstract void nextStep(String text, String countryCode);

    public AbstractPhoneDialogWrapper() {
        super(true);
        init();
        setTitle(getHead());
    }


    /**
     * 创建视图
     *
     * @return
     */
    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        if (isPassword()) {
            textField = new JPasswordField();
        } else {
            textField = new JTextField();
        }
        // 账号组件获取焦点
        textField.setFocusable(true);
        panel.add(textField, BorderLayout.CENTER);
        textField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (Objects.equals(e.getKeyCode(), 10)) {
                    // 输入了回车键
                    // 判断内容是否为空
                    String text = textField.getText();
                    String countryCode = countryCodeField.getText();
                    if (StrUtil.isBlank(text) || StrUtil.isBlank(countryCode)) {
                        Messages.showMessageDialog(getInfoMsg(), "提示", Messages.getInformationIcon());
                    } else {
                        close(CLOSE_EXIT_CODE);
                        // 进入下一步
                        nextStep(text, countryCode);
                    }
                }
            }
        });
        textField.grabFocus();

        countryCodeField = new JTextField("86");
        countryCodeField.setFocusable(false);
        panel.add(countryCodeField, BorderLayout.WEST);

        JLabel label = new JLabel();
        label.setText(getBottomMsg());
        panel.add(label, BorderLayout.SOUTH);
        return panel;
    }

    /**
     * 覆盖默认的ok/cancel按钮
     *
     * @return
     */
    @NotNull
    @Override
    protected Action[] createActions() {
        return new Action[]{};
    }
}
